﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Device.Location;
using Knihotoc_WP8.Settings;
using Knihotoc_WP8.Localization;

namespace Knihotoc_WP8.Pages
{
    /// <summary>
    /// Page displays a map and it gives the user the ability to choose 
    /// a position from the map and get it's coordinates.
    /// </summary>
    public partial class ChoosePositionFromMapPage : PhoneApplicationPage
    {
        private GeoCoordinate selectedPosition;
        private int bookID;

        public ChoosePositionFromMapPage()
        {
            this.InitializeComponent();
            selectedPosition = new GeoCoordinate();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            selectedPositionPushpin.Visibility = System.Windows.Visibility.Collapsed;
            map.Center = CurrentSettings.Instance.CurrentPosition;
            map.ZoomLevel = DefaultSettings.Instance.MapZoomLevel;

            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("BookID"))
            {
                bookID = Convert.ToInt32(NavigationContext.QueryString["BookID"]);
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            float lat = (float)selectedPosition.Latitude;
            float lon = (float)selectedPosition.Longitude;
            string description = "";
            int? place = null;
        }

        private void map_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Point tapPoint = e.GetPosition(this.map);
            selectedPosition = map.ViewportPointToLocation(tapPoint);

            positionLatitudeText.Text = selectedPosition.Latitude.ToString();
            positionLongitudeText.Text = selectedPosition.Longitude.ToString();

            selectedPositionPushpin.Visibility = System.Windows.Visibility.Visible;
            selectedPositionPushpin.Location = selectedPosition;
            map.Center = selectedPosition;
            if (map.ZoomLevel < DefaultSettings.Instance.MapZoomLevel)
            {
                map.ZoomLevel = DefaultSettings.Instance.MapZoomLevel;
            }
        }
    }
}